package com.ejlchina.tool.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
import com.adolf.androiddemo.databinding.FragmentScanCardBinding
import com.ejlchina.tool.adapter.DeviceAdapterRadio
import com.ejlchina.tool.base.BaseBindingFragment2
import com.ejlchina.tool.bean.BleDeviceBean
import com.ejlchina.tool.constant.COMMAND_DELAY_TIME_100
import com.ejlchina.tool.enums.ScanCardEnum
import com.ejlchina.tool.enums.SuccessFailEnum
import com.ejlchina.tool.singleton.GlobalConnectObject
import com.ejlchina.tool.util.COMMAND_SCAN_CARD_GET_DOWNWARD
import com.ejlchina.tool.util.COMMAND_SCAN_CARD_GET_UPWARD
import com.ejlchina.tool.util.COMMAND_SCAN_CARD_SET_DOWNWARD
import com.ejlchina.tool.util.COMMAND_SCAN_CARD_SET_UPWARD
import com.ejlchina.tool.util.bytearray.intToByteHexString
import com.ejlchina.tool.util.showToast
import com.ejlchina.tool.view.GridItemDecoration
import com.huyuhui.fastble.data.BleDevice
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class ScanCardFragment: BaseBindingFragment2<FragmentScanCardBinding>() {

    private var selectMode = ScanCardEnum.SCAN
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initViews()
        initData()
    }

    private fun initViews(){
        setHeadTitle("扫码刷卡")
        setRightIconVisibility(View.GONE)
        initListeners()
    }

    private fun initData(){
        initDevices()
    }

    private fun initListeners(){
        binding.layoutScan.setOnClickListener {
            selectMode = ScanCardEnum.SCAN
            setScanCardData(deviceAdapter.getSelectDevice())
        }
        binding.layoutCard.setOnClickListener {
            selectMode = ScanCardEnum.CARD
            setScanCardData(deviceAdapter.getSelectDevice())
        }
    }

    override fun whenReceiveCommand(bleDevice: BleDeviceBean,command: String, dataByteArray: ByteArray) {
        super.whenReceiveCommand(bleDevice,command, dataByteArray)
        dismissLoading()
        when(command){
            COMMAND_SCAN_CARD_GET_UPWARD -> {
                setUIData(ScanCardEnum.getFromValue(dataByteArray[0].toInt()))
            }
            COMMAND_SCAN_CARD_SET_UPWARD -> {
                if (dataByteArray.size > 1){
                    if (dataByteArray[1].toInt() == SuccessFailEnum.SUCCESS.value){
                        showToast("设置成功")
                        deviceAdapter.getSelectDevice().scanOrCard = selectMode.value
                        viewLifecycleOwner.lifecycleScope.launch {
                            delay(COMMAND_DELAY_TIME_100)
                            getScanCardData(deviceAdapter.getSelectDevice())
                        }
                    }else {
                        showToast("设置失败")
                    }
                }else {
                    showToast("设置失败")
                }
            }
        }
    }

    private fun initDevices(){
        deviceAdapter = DeviceAdapterRadio(GlobalConnectObject.getCurrentConnectDeviceList()){
            getScanCardData(it)
        }
        binding.rvDevices.apply {
            adapter = deviceAdapter
            layoutManager = GridLayoutManager(requireContext(), 3)
            if (itemDecorationCount == 0) {
                addItemDecoration(GridItemDecoration(3, 11, 11))
            }
        }
    }

    private fun setUIData(scanCardEnum: ScanCardEnum){
        selectMode = scanCardEnum
        binding.ivScanSelect.visibility = View.GONE
        binding.ivCardSelect.visibility = View.GONE
        when(selectMode){
            ScanCardEnum.SCAN -> {
                binding.ivScanSelect.visibility = View.VISIBLE
            }
            ScanCardEnum.CARD -> {
                binding.ivCardSelect.visibility = View.VISIBLE
            }
        }
    }

    private fun getScanCardData(bleDevice: BleDeviceBean){
        getCommandData(bleDevice,COMMAND_SCAN_CARD_GET_DOWNWARD)
    }

    private fun setScanCardData(bleDevice: BleDeviceBean){
        setCommandData(bleDevice,COMMAND_SCAN_CARD_SET_DOWNWARD,getResultData())
    }

    private fun getResultData(): String{
        return intToByteHexString(selectMode.value)
    }
    override fun getViewBinding(inflater: LayoutInflater, container: ViewGroup): FragmentScanCardBinding {
        return FragmentScanCardBinding.inflate(inflater,container,true)
    }
}